layui.use(['form','layer','laydate'],function(){
    var form = layui.form
        layer = parent.layer === undefined ? layui.layer : top.layer,
        laydate = layui.laydate,
        $ = layui.jquery;

    laydate.render({
        elem: '#birthday' //指定元素
    });
    laydate.render({
        elem: '#entryTime' //指定元素
    });

    $.ajax({
        type:"get",
        url:"http://localhost/admin/sysDept/getTableList",
        dataType:"json",
        success:function(data){
            var deptList = data.data;
            var str = '<option value="">请选择部门</option>';
            for(var i=0;i<deptList.length;i++){
                str += '<option value="'+deptList[i].id+'">'+deptList[i].deptName+'</option>';
            }
            $('.deptId').html(str);
            form.render();
        }
    })

    form.on('select(dept)',function(data){
        $.ajax({
            type:"get",
            url:"http://localhost/admin/sysRole/getRoleListByDeptId",
            data:{"deptId": data.value},
            dataType:"json",
            success:function(data){
                var deptList = data.data;
                var str = '<option value="">请选择角色</option>';
                for(var i=0;i<deptList.length;i++){
                    str += '<option value="'+deptList[i].id+'">'+deptList[i].roleName+'</option>';
                }
                $('.roleId').html(str);
                form.render();
            }
        })
    })

    form.on("submit(addUser)",function(data){
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        $.ajax({
            type: 'post',
            url: 'http://localhost/admin/sysStaff/insertStaff',
            data: form.val("formFilter"),
            dataType:"json",
            success:function(data){
                if(data.code == 0){
                    setTimeout(function(){
                        top.layer.close(index);
                        top.layer.msg(data.msg);
                        layer.closeAll("iframe");
                        //刷新父页面
                        parent.location.reload();
                    },2000);
                }else{
                    layer.msg(data.msg, {
                        icon: 5,
                        time: 1500 //2秒关闭（如果不配置，默认是3秒）
                    })
                }

            }
        })
        return false;
    })

    //格式化时间
    function filterTime(val){
        if(val < 10){
            return "0" + val;
        }else{
            return val;
        }
    }
    //定时发布
    var time = new Date();
    var submitTime = time.getFullYear()+'-'+filterTime(time.getMonth()+1)+'-'+filterTime(time.getDate())+' '+filterTime(time.getHours())+':'+filterTime(time.getMinutes())+':'+filterTime(time.getSeconds());

})